package com.ls.socketio.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.ls.socketio.handler.MessageEventHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author liShan
 * @date 2019/9/2 10:52
 */
@Configuration
public class BeanConfig {

    @Resource
    private MessageEventHandler messageEventHandler;

    @Bean
    public SocketIOServer socketIoServer() {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setHostname("localhost");
        config.setPort(9092);
        config.setRandomSession(true);
        SocketIOServer socketIoServer = new SocketIOServer(config);
        socketIoServer.addListeners(messageEventHandler);
        return socketIoServer;
    }

    @Bean
    public CustomObjectMapper createCustomObjectMapper(){
        CustomObjectMapper mapper = new CustomObjectMapper();
        // 设置在反序列化时忽略在JSON字符串中存在，而在Java中不存在的属性
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // 设置Jackson序列化时只包含不为空的字段
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper;
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}
